import { getAddressList } from '@/api/address'
import { getInfo, setInfo } from '@/utils/storage'
export default {
  namespaced: true,
  state () {
    return {
      userInfo: getInfo(),
      addressList: [],
      indexAddress: ''
    }
  },
  getters: {
  },
  mutations: {
    setUserInfo (state, obj) {
      state.userInfo = obj
      setInfo(obj)
    },
    setAddressList (state, newList) {
      state.addressList = newList
    },
    setIndexAddress (state, newIndexAddress) {
      state.indexAddress = newIndexAddress
    }
  },
  actions: {
    async getAddressListAction (context) {
      const res = await getAddressList()
      const index = res.data.find(item => item.index)
      context.commit('setAddressList', res.data)
      context.commit('setIndexAddress', index)
    },
    logout (context) {
      context.commit('setUserInfo', {})
      context.commit('setAddressList', [])
      context.commit('cart/setCartList', [], { root: true })
    }
  }
}
